<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                        <el-form-item label="英文名称" prop="keyName">
                            <el-input
                                    v-model="queryParams.keyName"
                                    placeholder="请输入英文名称"
                                    clearable
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="中文名称" prop="chineseName">
                            <el-input
                                    v-model="queryParams.chineseName"
                                    placeholder="请输入中文名称"
                                    clearable
                                    @keyup.enter.native="handleQuery"
                            />
                        </el-form-item>
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                                <el-option
                                        v-for="dict in dict.type.sys_common_status"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['kettle:param:add']"
                >新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['kettle:param:edit']"
                >修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['kettle:param:remove']"
                >删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['kettle:param:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

      <el-table border="border" max-height="550px" v-loading="loading" :data="paramList"
                @selection-change="handleSelectionChange" @row-click="rowClick"
                @sort-change="handleSortChange" :row-style="tableRowStyle" ref="param">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column
          v-hasPermi="['kettle:param:query']"
          label="操作" align="center" width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleUpdate(scope.row,false)"
              v-hasPermi="['kettle:param:query']"
            >详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="英文名称" align="center" prop="keyName" width="150" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="中文名称" align="center" prop="chineseName" width="150" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="参数类型" align="center" width="150" prop="ftype" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">{{ scope.row.ftype === 1 ? '无' : scope.row.ftype === 2 ? 'SQL':"" }}</template>
        </el-table-column>
        <el-table-column label="数据类型" width="150" align="center" prop="datatype" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">{{ scope.row.datatype === 1 ? '字符串' : scope.row.datatype === 2 ? '整数':
            scope.row.datatype === 3 ? "布尔": scope.row.datatype === 4 ? "年月日":"日期"}}
          </template>
        </el-table-column>
        <el-table-column label="是否必填" align="center" prop="required">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.required"/>
          </template>
        </el-table-column>
        <el-table-column label="默认值" align="center" prop="defaultValue" width="150" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="顺序" align="center" prop="orderNum" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_common_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="添加时间" align="center" prop="createTime" width="180" sortable="custom"
                         :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="添加人" align="center" width="150" prop="createBy" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="编辑时间" align="center" prop="updateTime" width="180" sortable="custom"
                         :sort-orders="['descending', 'ascending']">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编辑人" align="center" width="150" prop="updateBy" :show-overflow-tooltip="true"
                         sortable="custom" :sort-orders="['descending', 'ascending']"/>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" sortable="custom"
                         :sort-orders="['descending', 'ascending']"/>
      </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改kettle参数信息对话框 -->
        <el-dialog  :title="title" :visible.sync="open" width="800px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="英文名称" prop="keyName">
                  <el-input clearable v-model="form.keyName" placeholder="请输入英文名称"/>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="中文名称" prop="chineseName">
                  <el-input clearable v-model="form.chineseName" placeholder="请输入中文名称"/>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="参数类型" prop="ftype">
                  <el-select v-model="form.ftype" placeholder="请选择参数类型" clearable>
                    <el-option
                      v-for="dict in ftypes"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    />
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="数据类型" prop="datatype">
                  <el-select v-model="form.datatype" placeholder="请选择数据类型" clearable>
                    <el-option
                      v-for="dict in datatypes"
                      :key="dict.value"
                      :label="dict.label"
                      :value="parseInt(dict.value)"
                    />
                  </el-select>
                </el-form-item>
              </el-col>


              <el-col :span="12">
                <el-form-item label="是否必填" prop="required">
                  <el-radio-group v-model="form.required">
                    <el-radio
                      v-for="dict in dict.type.sys_yes_no"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                    >{{dict.label}}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="默认值" prop="defaultValue">
                  <el-input clearable v-model="form.defaultValue" placeholder="请输入默认值"/>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="顺序" prop="orderNum">
                  <el-input-number style="width: 100%" v-model="form.orderNum" controls-position="right" :min="0"/>
                </el-form-item>
              </el-col>


              <el-col :span="12">
                <el-form-item label="状态" prop="status">
                  <el-radio-group v-model="form.status">
                    <el-radio
                      v-for="dict in dict.type.sys_common_status"
                      :key="dict.value"
                      :label="parseInt(dict.value)"
                    >{{dict.label}}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input maxlength="500" v-model="form.remark" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" v-if="isUpdate" :loading="butloading" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { listParam, getParam, delParam, addParam, updateParam } from "@/api/kettle/param";
    import { getTableRowStyle } from "@/utils/yss";
    export default {
        name: "Param",
            dicts: ['sys_common_status', 'sys_yes_no'],
        data() {
            return {
              butloading:false,
              isUpdate:false,
              ftypes: [
                {
                  value: 1,
                  label: "无"
                },
                {
                  value: 2,
                  label: "SQL"
                }
              ],
              datatypes: [
                {
                  value: 1,
                  label: "字符串"
                },
                {
                  value: 2,
                  label: "整数"
                } ,
                {
                  value: 3,
                  label: "布尔"
                },
                {
                  value: 4,
                  label: "年月日"
                },
                {
                  value: 5,
                  label: "日期"
                }
              ],
                // 遮罩层
                loading: true,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // kettle参数信息表格数据
                    paramList: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 50,
                    id: null,
                    keyName: null,
                    chineseName: null,
                    ftype: null,
                    datatype: null,
                    required: null,
                    defaultValue: null,
                    orderNum: null,
                    status: null,
        },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
              keyName: [
                { required: true, message: "英文名称不能为空", trigger: "blur" }
              ],
              chineseName: [
                { required: true, message: "中文名称不能为空", trigger: "blur" }
              ],
              ftype: [
                { required: true, message: "参数类型不能为空", trigger: "blur" }
              ],
              datatype: [
                { required: true, message: "数据类型不能为空", trigger: "blur" }
              ],
              defaultValue: [
                { required: true, message: "默认值不能为空", trigger: "blur" }
              ],
              orderNum: [
                { required: true, message: "顺序不能为空", trigger: "blur" }
              ]
            }
        };
        },
        created() {
            this.getList();
        },
        methods: {
            /** 查询kettle参数信息列表 */
            getList() {
                this.loading = true;
                listParam(this.queryParams).then(response => {
                    this.paramList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                        id: null,
                        keyName: null,
                        chineseName: null,
                        ftype: null,
                        datatype: null,
                        required: null,
                        defaultValue: null,
                        orderNum: null,
                        status: null,
                        createTime: null,
                        createBy: null,
                        updateTime: null,
                        updateBy: null,
                        remark: null
            };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length!==1
                this.multiple = !selection.length
            },
            /** 排序触发事件 */
            handleSortChange(column, prop, order) {
                this.queryParams.orderByColumn = column.prop;
                this.queryParams.isAsc = column.order;
                this.getList();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.title = "添加参数信息";
                this.isUpdate =true;
                this.form.required = parseInt(this.dict.type.sys_yes_no[0].value);
                this.form.status = parseInt(this.dict.type.sys_common_status[0].value);
                this.form.ftype =  parseInt(this.ftypes[0].value);
                this.form.datatype =  parseInt(this.datatypes[0].value);
            },
            /** 修改按钮操作 */
            handleUpdate(row,isUpdate) {
                this.reset();
                const id = row.id || this.ids
                getParam(id).then(response => {
                    this.form = response.data;
                this.open = true;
                this.title = "修改参数信息";
              this.isUpdate =true;
              if(isUpdate == false){
                this.title = "参数信息详情";
                this.isUpdate =false;
              }
            });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                      this.butloading=true;
                        if (this.form.id != null) {
                            updateParam(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        } else {
                            addParam(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {this.butloading=false});
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除数据项？').then(function() {
                    return delParam(ids);
                }).then(() => {
                    this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
            },
    /** 导出按钮操作 */
    handleExport() {
        this.download('kettle/param/export', {
                ...this.queryParams
    }, `param_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({ row, rowIndex}){
        return getTableRowStyle(row,rowIndex,this.ids);
    },
    /** 单击选中行数据 */
    rowClick(row) {
        this.$refs.param.toggleRowSelection(row);
    }
    }
    };
</script>
